public class StudentTest {
    public static void main(String[] args) {
        Student[] arr = new Student[3];

        arr[0] = new Student(1, "张三", 18);
        arr[1] = new Student(2, "李四", 19);
        arr[2] = new Student(3, "王五", 20);

        Student s = new Student(4, "赵六", 21);

        if (contain(arr, s.getId()))
            System.out.println("学号已存在,添加失败!");
        else {
            int count=countele(arr);
            if(arr.length==count)
            {
                Student[] newarr=creatNewarr(arr);
                newarr[count]=s;
                printArr(newarr);

                int index=getIndex(newarr,2);
                if(index>=0)
                    newarr[index]=null;
                else
                    System.out.println("id不存在，删除失败!");

                printArr(newarr);

                index=getIndex(newarr,3);
                if(index>=0)
                    arr[index].setAge(arr[index].getAge()+1);
                else
                    System.out.println("id不存在，查找失败");

                printArr(newarr);
            }
            else
            {
                arr[count]=s;
                printArr(arr);

                int index=getIndex(arr,2);
                if(index>=0)
                    arr[index]=null;
                else
                    System.out.println("id不存在，删除失败!");

                printArr(arr);

                index=getIndex(arr,3);
                if(index>=0)
                    arr[index].setAge(arr[index].getAge()+1);
                else
                    System.out.println("id不存在，查找失败!");

                printArr(arr);
            }
        }
    }

    public static int getIndex(Student[] arr,int id)
    {
        for (int i = 0; i < arr.length; i++)
            if(arr[i]!=null&&arr[i].getId()==id)
                return i;

        return -1;
    }
    public static void printArr(Student[] arr)
    {
        for (int i = 0; i < arr.length; i++)
            if(arr[i]!=null)
                System.out.println(arr[i].getId()+" "+arr[i].getName()+" "+arr[i].getAge());
    }

    public static Student[] creatNewarr(Student[] arr)
    {
        Student[] newarr=new Student[arr.length+1];

        for (int i = 0; i < arr.length; i++)
            newarr[i]=arr[i];

        return newarr;
    }

    public static int countele(Student[] arr)
    {
        int count=0;
        for (int i = 0; i < arr.length; i++)
            if(arr[i]!=null)
                count++;

        return count;
    }

    public static boolean contain(Student[] arr,int id)
    {
        for (int i = 0; i < arr.length; i++)
            if(arr[i]!=null&&arr[i].getId()==id)
                return true;

        return false;
    }
}